/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BookCategory;
import java.sql.*;
import java.util.Vector;
/**
 *
 * @author mishra
 */
public class Query {
    static Connection con=null;
public static int add(BookCategoryBean bcb) throws SQLException
{
    int i=0,k=0;
    try{
        con=DBConnection.Connection.getConnection();
        PreparedStatement pre=con.prepareStatement("insert into book_category values(bookcategoryid.nextval,?,sysdate)");
        pre.setString(1, bcb.getBookCategory());
        i=pre.executeUpdate();
       if(i!=0)
       {
           k=getMaxId();
       }
    }catch(Exception e){
        System.out.println(e);
    }finally{con.close();}
    return k;
}
public static int getMaxId() throws SQLException
{
    int i=0;
    try
    {
        con=DBConnection.Connection.getConnection();
        PreparedStatement pre=con.prepareStatement("select max(book_category_id) from book_category");
        ResultSet rs=pre.executeQuery();
        if(rs.next())
        {
            i=rs.getInt(1);
        }
        
    }catch(Exception e)
    {
        System.out.println(e);
    }finally{
        con.close();
    }
    return i;
}
public static Vector viewAll() throws SQLException
{
    Vector<Object> list=new Vector<Object>();
    try{
        con=DBConnection.Connection.getConnection();
        PreparedStatement pre=con.prepareStatement("select * from book_category order by book_category_id");
        ResultSet rs=pre.executeQuery();
        while(rs.next())
        {
            BookCategoryBean bcb=new BookCategoryBean();
            bcb.setBookCategoryId(rs.getInt(1));
            bcb.setBookCategory(rs.getString(2));
            bcb.setEntryDate(rs.getDate(3));
            list.add(bcb);
        }
    }catch(Exception e){
        System.out.println(e);
    }finally{con.close();}
    return list;
    }
public static int edit(BookCategoryBean bcb) throws SQLException
{
    int i=0;
    try{
        con=DBConnection.Connection.getConnection();
        PreparedStatement pre=con.prepareStatement("update BOOK_CATEGORY set BOOK_CATEGORY=?, ENTRY_DATE=sysdate where BOOK_CATEGORY_ID=?");
        pre.setString(1, bcb.getBookCategory());
        pre.setInt(2, bcb.getBookCategoryId());
        i=pre.executeUpdate();
    }catch(Exception e){
        System.out.println(e);
    }finally{
        con.close();
    }return i;
}
public static int delete(int id) throws SQLException
{
    int i=0;
    try{
        con=DBConnection.Connection.getConnection();
        PreparedStatement pre=con.prepareStatement("delete from BOOK_CATEGORY where BOOK_CATEGORY_ID=?");
        pre.setInt(1, id);
        i=pre.executeUpdate();
    }catch(Exception e){
        System.out.println(e);
    }finally{
        con.close();
    }return i;
}
}
